package com.hrprojectback.controller.XinXiGuanLi;

import com.alibaba.fastjson.JSON;
import com.hrprojectback.dto.*;
import com.hrprojectback.enums.StatusEnum;
import com.hrprojectback.service.XinXiGuanLiService.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public ResponseDTO getUser(UserInfo userInfo) {
        //获取员工的总数量
        Integer userCounts = userService.getUserCounts(userInfo.getUsername(), userInfo.getName());
        //开始页
        int pageStart = (userInfo.getPageNum() - 1) * userInfo.getPageSize();
        List<User> users = userService.getUser(userInfo.getUsername(), userInfo.getName(), pageStart, userInfo.getPageSize());
        if (users.size() == 0) {
            return Response.error2(StatusEnum.STAFF_NOT_EXIST2.getCode(), StatusEnum.STAFF_NOT_EXIST2.getMessage(), users);
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("userCounts", userCounts);
        res.put("data", users);
//        String YuanGong = JSON.toJSONString(res);
        return Response.success(res);
    }

    /**
     * 新增员工接口
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public ResponseDTO addDept(@RequestBody User user) {
        //默认是生效
        user.setStatus("1");
        int i = userService.addUser(user);

        if (i > 0) {
            return Response.success(user);
        }
        return Response.error();
    }

    /**
     * 根据员工工号去查询需要编辑的部门信息
     *
     * @param username
     * @return
     */
    @RequestMapping("/getEditUser")
    public String editUser(String username) {
        //获取需要修改的员工的信息
        User getEditUser = userService.getEditUser(username);
        String editUser = JSON.toJSONString(getEditUser);
        return editUser;
    }

    /**
     * 编辑员工信息
     *
     * @param user
     * @return
     */
    @PutMapping("/editUser")
    public ResponseDTO editUser(@RequestBody User user) {
        int i = userService.editUser(user);
        if (i > 0) {
            return Response.success(user);
        }
        return Response.error();
    }

    /**
     * 根据员工工号username删除员工
     *
     * @param username
     * @return
     */
    @DeleteMapping("/deleteUsername")
    public ResponseDTO deleteUser(String username) {
        int i = userService.deleteUser(username);
        if (i > 0) {
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 根据员工工号username生效员工
     *
     * @param username
     * @param status
     * @return
     */
    @RequestMapping("/userStateShengXiao")
    public ResponseDTO userStateShengXiao(@RequestParam("username") String username, @RequestParam("status") int status) {
        if (status == 1) {
            return Response.error(StatusEnum.STAFF_IS_USER_SHENG_XIAO);
        }
        int shengXiao = userService.ShengXiao(username);
        if (shengXiao > 0) {
            return Response.successStatus(shengXiao);
        }
        return Response.error(StatusEnum.STATUS_SHENG_XIAO_ERROR);
    }

    /**
     * 根据员工工号username失效员工
     *
     * @param username
     * @param status
     * @return
     */
    @RequestMapping("/userStateShiXiAo")
    public ResponseDTO userStateShiXiAo(@RequestParam("username") String username, @RequestParam("status") int status) {
        if (status == 0) {
            return Response.error(StatusEnum.STAFF_IS_USER_SHI_XIAO);
        }
        int shiXiAo = userService.YiShiXiAo(username);
        if (shiXiAo > 0) {
            return Response.successStatus2(shiXiAo);
        }
        return Response.error(StatusEnum.STATUS_SHI_XIAO_ERROR);
    }

    /**
     * 通过员工工号批量删除员工
     *
     * @param usernames
     * @return
     */
    @DeleteMapping("/BatchDeleteUser")
    public ResponseDTO BatchDeleteUser(@RequestBody List<String> usernames) {
        if (usernames.size() > 0) {
            for (String username : usernames) {
                deleteUser(username);
            }
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 通过员工工号批量生效员工
     *
     * @param usernames
     * @return
     */
    @RequestMapping("/BatchShengXiaoUser")
    public ResponseDTO BatchShengXiaoUser(@RequestBody List<String> usernames) {
        if (usernames.size() == 0) {
            return Response.success();
        }
        if (usernames.size() > 0) {
            ArrayList statusList = new ArrayList();
            for (String username : usernames) {
                //查看员工的当前状态
                int userStatus = userService.getStatus(username);
                if (userStatus == 1) {
                    statusList.add(userStatus);
                }
            }
            if (statusList.size() > 0) {
                return Response.error(StatusEnum.SHENG_XIAO_STAFF_IS_EXIST);
            }
            for (String username : usernames) {
                userService.ShengXiao(username);
            }
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 通过员工工号批量失效员工
     *
     * @param usernames
     * @return
     */
    @RequestMapping("/BatchShiXiaoUser")
    public ResponseDTO BatchShiXiaoUser(@RequestBody List<String> usernames) {
        if (usernames.size() == 0) {
            return Response.success();
        }
        if (usernames.size() > 0) {
            ArrayList statusList = new ArrayList();
            for (String username : usernames) {
                //查看员工的当前状态
                int userStatus = userService.getStatus(username);
                if (userStatus == 0) {
                    statusList.add(userStatus);
                }
            }
            if (statusList.size() > 0) {
                return Response.error(StatusEnum.SHI_XIAO_STAFF_IS_EXIST);
            }
            for (String username : usernames) {
                userService.YiShiXiAo(username);
            }
            return Response.success();
        }
        return Response.error();
    }
}
